﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using Common.Views;
using ElectrosLtdApplication.Models;

namespace ElectrosLtdApplication.Controllers
{
    public class HomeController : MenusController
    {
        [Authorize]
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to Electros Ltd website!";
            
            HomeModel hm = new HomeModel();
            return View(hm);
        }

        [Authorize]
        public PartialViewResult GetProductsByCategory(int cId)
        {
            List<Product> products = new ProductsServ.ProductsServiceClient().GetProductsByCategory(cId).ToList();
            ViewBag.Message = "Results for the chosen category: ";
            return PartialView("_products", products);
        }

        [Authorize]
        public PartialViewResult SearchName(string keyword)
        {
            List<Product> products = new ProductsServ.ProductsServiceClient().GetProductsByName(keyword).ToList();
            ViewBag.Message = "Results for '"+keyword+"': ";
            return PartialView("_products", products);
        }

        [Authorize]
        public PartialViewResult SearchPrice(decimal min, decimal max)
        {
            List<Product> products = new ProductsServ.ProductsServiceClient().GetProductsByPrice(min, max).ToList();
            ViewBag.Message = "Results for the chosen price range: ";
            return PartialView("_products", products);
        }

        public PartialViewResult SortByPriceAsc()
        {
            List<Product> products = new ProductsServ.ProductsServiceClient().SortByPriceAsc().ToList();
            ViewBag.Message = "Results sorted by price in ascending order: ";
            return PartialView("_products", products);
        }

        public PartialViewResult SortByPriceDesc()
        {
            List<Product> products = new ProductsServ.ProductsServiceClient().SortByPriceDesc().ToList();
            ViewBag.Message = "Results sorted by price in descending order: ";
            return PartialView("_products", products);
        }

        public PartialViewResult SortByDate()
        {
            List<Product> products = new ProductsServ.ProductsServiceClient().SortByDateAsc().ToList();
            ViewBag.Message = "Results sorted by date (latest first): ";
            return PartialView("_products", products);
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
